package com.lemon.cloud.admin.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.admin.service.SysPostService;
import com.lemon.cloud.comm.constants.enums.LogBusType;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.comm.po.SysPost;
import com.lemon.cloud.log.annotation.SysLog;
import com.lemon.cloud.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("post")
@Tag(name = "岗位接口")
public class SysPostController {

    @Autowired
    private SysPostService postService;

    @GetMapping("/list")
    @Operation(
            summary = "获取所有岗位列表",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    public ResultMsg<List<SysPost>> getPost() {
        return ResultMsg.resultSuccess(postService.lambdaQuery()
                .select(SysPost::getId, SysPost::getName, SysPost::getCode, SysPost::getRemark)
                .list());
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @Operation(
            summary = "分页查询",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('sys_post_get')")
    public ResultMsg<IPage<SysPost>> getPostByPage(@ParameterObject Page<SysPost> page,@ParameterObject SysPost post) {
        return ResultMsg.resultSuccess(postService.getPostByPage(page, post));
    }

    /**
     * 通过id查询岗位信息表
     *
     * @param id id
     * @return ResultMsg
     */
    @Operation(
            summary = "通过id查询岗位信息表",
            parameters = {@Parameter(name = "id",description = "岗位id",in = ParameterIn.PATH,required = true)},
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys_post_get')")
    public ResultMsg<SysPost> getById(@PathVariable("id") Long id) {
        return ResultMsg.resultSuccess(postService.getById(id));
    }

    /**
     * 新增岗位信息表
     *
     * @param sysPost 岗位信息表
     * @return ResultMsg
     */
    @Operation(
            summary = "新增岗位信息表",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @SysLog(value = "新增岗位信息表", businessType = LogBusType.ADD)
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys_post_add')")
    public ResultMsg<Boolean> save(@Parameter @RequestBody SysPost sysPost) {
        sysPost.setCreateTime(LocalDateTime.now());
        sysPost.setUpdateTime(LocalDateTime.now());
        sysPost.setCreateBy(SecurityUtils.getUser().getUsername());
        sysPost.setUpdateBy(SecurityUtils.getUser().getUsername());
        return ResultMsg.resultSuccess(postService.save(sysPost));
    }

    /**
     * 修改岗位信息表
     *
     * @param sysPost 岗位信息表
     * @return ResultMsg
     */
    @Operation(
            summary = "修改岗位信息表",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @SysLog(value = "修改岗位信息表", businessType = LogBusType.MODIFY)
    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys_post_edit')")
    public ResultMsg<Boolean> updateById(@Parameter @RequestBody SysPost sysPost) {
        sysPost.setUpdateTime(LocalDateTime.now());
        sysPost.setUpdateBy(SecurityUtils.getUser().getUsername());
        return ResultMsg.resultSuccess(postService.updateById(sysPost));
    }

    /**
     * 通过id删除岗位信息表
     *
     * @param id id
     * @return ResultMsg
     */
    @Operation(
            summary = "通过id删除岗位信息表",
            parameters = {@Parameter(name = "id",required = true,in = ParameterIn.PATH)},
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @SysLog(value = "通过id删除岗位信息表", businessType = LogBusType.DELETE)
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys_post_del')")
    public ResultMsg<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultMsg.resultSuccess(postService.delPost(id));
    }
}
